package com.roger.springcloudGreenwich.service.impl;

import com.orbitz.consul.AgentClient;
import com.orbitz.consul.Consul;
import com.orbitz.consul.model.agent.Member;
import com.roger.springcloudGreenwich.connection.ConsulClientPool;
import com.roger.springcloudGreenwich.service.ConsulService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.List;

/**
 * Created by admin on 2020/4/7.
 */
@Service
@Slf4j
public class ConsulServiceImpl implements ConsulService {
    @Value("${spring.cloud.consul.discovery.acl-token}")
    private String aclToken;

    @Value("${spring.cloud.consul.host}")
    private String host;

    @Value("${spring.cloud.consul.port}")
    private int port;

    @Autowired
    private ConsulClientPool consulClientPool;

    @Override
    public void deleteConsulServiceId(String serviceId) {
        String key = host + ":" + port;
        AgentClient agentClient = consulClientPool.getConsulClientMap(host, port, aclToken).get(key);
        agentClient.deregister(serviceId);
        log.info("delete over");
    }

    @Override
    public List<Member> getConsulMember() {
        String key = host + ":" + port;
        AgentClient agentClient = consulClientPool.getConsulClientMap(host, port, aclToken).get(key);
        List<Member> list = agentClient.getMembers();
        return list;
    }

}

